package com.benmei.weike.util;

import com.benmei.weike.common.Constants;
import com.benmei.weike.exception.ServerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public class PicUnion {
    private static Logger logger = LoggerFactory.getLogger(PicUnion.class);

    /**
     * 导入本地图片到缓冲区
     */
    public BufferedImage loadImageLocal(String imgName) {
        try {
            return ImageIO.read(new File(imgName));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /**
     * 导入网络图片到缓冲区
     */
    public BufferedImage loadImageUrl(String imgName) {
        try {
            URL url = new URL(imgName);
            return ImageIO.read(url);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /**
     * 生成新图片到本地
     */
    public void writeImageLocal(String newImage, BufferedImage img) {
        if (newImage != null && img != null) {
            try {
                File outputfile = new File(newImage);
                ImageIO.write(img, "jpg", outputfile);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d) {
        try {
            int w = b.getWidth();
            int h = b.getHeight();
            Graphics2D g = d.createGraphics();
            g.drawImage(b, 111, 585, w, h, null);
            g.dispose();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return d;
    }

    /**
     * 生成我的邀请码（成人版）
     *
     * @param memb_id 学生id
     * @return
     */
    public static String mergeA_InvitationCard(Integer memb_id, String url) {
        String myCardUrl = null;

        try {
            // 1. 生成我的二维码图片
            String qrCodeImageFilePathname = Constants.QrCodeImageFilePath + "qrCode" + memb_id + ".png";//我的二维码存放路径和文件名称
            String backgroundImageFilePathname = Constants.A_inviteCardBackgroundPathname;//青少版，我的邀请卡底图图片路径和文件名称
            File qrCodeFile = QrCodeCreateUtil.draQRCode(url, qrCodeImageFilePathname);

            // 2. 合并图片，将二维码与底图合并
            BufferedImage qrCodeBuffImage = ImageIO.read(qrCodeFile);// 学生的个人二维码
            BufferedImage backgroundBuffImage = ImageIO.read(new File(backgroundImageFilePathname));// 邀请卡底图
            int w = 210;//qrCodeBuffImage.getWidth();//二维码的宽
            int h = 210;//backgroundBuffImage.getHeight();//二维码的高
            int offsetX = 111;
            int offsetY = 585;
            Graphics2D graphics2D = backgroundBuffImage.createGraphics();// 2D画笔
            graphics2D.drawImage(qrCodeBuffImage, offsetX, offsetY, w, h, null);
            graphics2D.dispose();

            String invitationCardFilePath = Constants.inviteCardPath + "/invitationCard_" + memb_id + ".jpg";// 合成后的图片地址（本地地址）
            File myCardFile = new File(invitationCardFilePath);// 合成后的最终图片
            ImageIO.write(backgroundBuffImage, "jpg", myCardFile);

            // 3. 上传到阿里云
            myCardUrl = AliyunOssUtil.uploadInviteCode(myCardFile, memb_id);

            // 4. 删除图片
            qrCodeFile.delete();
            myCardFile.delete();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServerException("邀请卡创建失败");
        }
        return myCardUrl;
    }

    /**
     * 生成我的邀请码（青少版）
     *
     * @param memb_id 学生id
     * @return
     */
    public static String mergeTeenagerInvitationCard(Integer memb_id, String url) {
        String myCardUrl = null;
        try {
            // 1. 生成我的二维码图片
            String qrCodeImageFilePathname = Constants.QrCodeImageFilePath + "qrCode" + memb_id + ".png";//我的二维码存放路径和文件名称
            String backgroundImageFilePathname = Constants.T_inviteCardBackgroundPathname;//青少版，我的邀请卡底图图片路径和文件名称
            File qrCodeFile = QrCodeCreateUtil.draQRCode(url, qrCodeImageFilePathname);

            // 2. 合并图片，将二维码与底图合并
            BufferedImage qrCodeBuffImage = ImageIO.read(qrCodeFile);// 学生的个人二维码
            BufferedImage backgroundBuffImage = ImageIO.read(new File(backgroundImageFilePathname));// 邀请卡底图
            int w = 120;// qrCodeBuffImage.getWidth();二维码的宽
            int h = 120;//backgroundBuffImage.getHeight();二维码的高
            int offsetX = 38;
            int offsetY = 708;

            Graphics2D graphics2D = backgroundBuffImage.createGraphics();// 2D画笔
            graphics2D.drawImage(qrCodeBuffImage, offsetX, offsetY, w, h, null);
            graphics2D.dispose();

            String invitationCardFilePath = Constants.inviteCardPath + "/teenagerInvitationCard_" + memb_id + ".jpg";// 合成后的图片地址（本地地址）
            File myCardFile = new File(invitationCardFilePath);
            ImageIO.write(backgroundBuffImage, "jpg", myCardFile);

            // 3. 上传到阿里云
            myCardUrl = AliyunOssUtil.uploadInviteCode(myCardFile, memb_id);

            // 4. 删除图片
            qrCodeFile.delete();
            myCardFile.delete();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServerException("邀请卡创建失败");
        }
        return myCardUrl;
    }


    public static void main(String[] args) {

//        PicUnion tt = new PicUnion();
//
//        BufferedImage b = tt.loadImageLocal("D:\\code.jpg");// 二维码图片
//        BufferedImage d = tt.loadImageLocal("D:\\invite.jpg");// 底图
//
//        tt.writeImageLocal("D:\\1.png", tt.modifyImagetogeter(b, d));
        //将多张图片合在一起
//        System.out.println("success");
        Integer memb_id = 123;
        String qrCodeImageFilePath = "D:\\666.png";
        String backgroundImageFilePath = "D:\\teenager-invite-card-bg.jpg";
//        String backgroundImageFilePath="d:\\invite.jpg";
//        String resultFile = mergeTeenagerInvitationCard(memb_id, qrCodeImageFilePath, backgroundImageFilePath);
//        System.out.println(resultFile);
    }

}
